package com.qiangesoft.sms.aliyun.config;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 阿里云短信配置
 *
 * @author qiangesoft
 * @date 2024-04-30
 */
@Configuration
@EnableConfigurationProperties(AliyunSmsProperties.class)
@ConditionalOnProperty(prefix = "sms.aliyun", name = "enabled", havingValue = "true")
public class AliyunSmsConfiguration {

    private static final String PRODUCT = "Dysmsapi";

    private static final String ENDPOINT = "dysmsapi.aliyuncs.com";

    @Autowired
    private AliyunSmsProperties aliyunSmsProperties;

    @Bean
    @ConditionalOnMissingBean
    public IAcsClient acsClient() {
        String regionId = aliyunSmsProperties.getRegionId();
        String accessKeyId = aliyunSmsProperties.getAccessKeyId();
        String accessKeySecret = aliyunSmsProperties.getAccessKeySecret();

        IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint(regionId, PRODUCT, ENDPOINT);
        return new DefaultAcsClient(profile);
    }

}
